package android.apps.activity;

import android.app.Activity;
import android.apps.R;
import android.apps.bll.RawBLL;
import android.apps.view.RawView;
import android.content.Intent;
import android.graphics.Point;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.io.File;
import java.net.URI;

public class RawViewActivity extends Activity implements View.OnClickListener, View.OnTouchListener {

    Button browserButton;
    CheckBox cursorCheckBox;
    EditText pathEdit;
    RawView rawView;
    String path_image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rawview);
        path_image = getIntent().getStringExtra("path");
        Log.e("LCY", "path_image=="+path_image);
        browserButton = (Button) findViewById(R.id.btnBrowser);
        cursorCheckBox = (CheckBox) findViewById(R.id.chckCursor);
        pathEdit = (EditText) findViewById(R.id.edtPath);
        rawView = (RawView) findViewById(R.id.viewDraw);
        rawView.setOnTouchListener(this);
//        browserButton.setOnClickListener(this);
        if (path_image!=null&&!path_image.equals("")){
            RawBLL rawBll = new RawBLL(path_image);
            rawView.rawBll = rawBll;
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("*/*");
        intent.addCategory(Intent.CATEGORY_OPENABLE);

        try {
            startActivityForResult(Intent.createChooser(intent, "请选择Raw文件"), 1);
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(this, "Please install a File Manager.", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    // Get the Uri of the selected file
                    Uri uri = data.getData();
                    String type = data.getType();
                    if (uri != null) {
                        String path = uri.toString();
                        if (path.toLowerCase().startsWith("file://")) {
                            // Selected file/directory path is below
                            path = (new File(URI.create(path))).getAbsolutePath();
                            pathEdit.setText(path);
                            Log.e("LCY", "路径==" + path);
                            RawBLL rawBll = new RawBLL(path);
                            rawView.rawBll = rawBll;
                        }

                    }
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            rawView.myTouch(new Point((int) event.getX(), (int) event.getY()));
        }
        return true;
    }
}
